//
//  XYWebModel.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/12.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYWebModel: NSObject,NSCoding {
    
    var access_token:String?
     //token的过期时间
    var expires_date:NSDate?
    var expires_in:NSTimeInterval = 0 {
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //token过期时间,已经不使用了
    //var remind_in:String?
    
    //当前授权用户的UID
    var uid:String?
    
    //用户头像地址（大图），180×180像素
    var avatar_large:String?
    //用户名称
    var screen_name:String?
    
    
    
    
//初始化字典的方法
    init(dict:[String : AnyObject]){
        super.init()
        //KVC给属性赋值
        setValuesForKeysWithDictionary(dict)
    }
    
    //过滤掉多余的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {  }
    
    //重写打印字典的内容
    override var description:String{
        let keys = ["access_token","expires_date","uid","screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    //获取用户信息
    func loadUserInfo(complition:(error:NSError?) -> ()){
        XYSessionManager.sharedManager.loadUserInfo(access_token!, uid: uid!) { (dict, error) -> () in
            if error != nil {
                complition(error: error)
                return
            }
            //请求用户信息肯定完成,保存用户信息
            self.avatar_large = dict!["avatar_large"] as? String
            self.screen_name = dict!["screen_name"] as? String
            self.saveUserAccount()
            //调用闭包
            complition(error: nil)
        }
    }
    
    
    //定义保存路径
    static let path = (NSSearchPathForDirectoriesInDomains( .DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.pilst")
    
    //对外的存取方法
    //存
    func saveUserAccount(){
        NSKeyedArchiver.archiveRootObject(self, toFile: XYWebModel.path)
    }
    
    //取
    class func  loadUserAccount() -> XYWebModel?{
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(XYWebModel.path) as? XYWebModel{
            //如果能取出来 就和现在时间做比较
            if account.expires_date?.compare(NSDate( )) == NSComparisonResult.OrderedDescending{
                return account
            }
        }
        return nil
    }
    
    
 //归档解档的方式--------------------------------------------------------------------
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
 //归档解档的方式--------------------------------------------------------------------
}
